<?php
namespace app\admin\controller;
use app\admin\controller\BaseController;
use app\common\errcode\AdminErrCode;
use app\common\errcode\AdminErrMsg;
use think\Loader;

class ToolController extends BaseController
{
    protected $toolModel;

    public function __construct()
    {
        parent::__construct();
        $this->toolModel = new \app\admin\model\ToolModel();
    }

    /**
     * 新建工具
     * @param string    $icon   图标
     * @param int       $catid  分类id
     * @param string    $name   名称
     * @param string    $path   路径
     * @param string    $desc   描述
     * @param string    $tags   标签,例: 'AI,聊天'
     */
    public function create()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Tool');
                $check = $validate->scene('create')->check($data);
                if(!$check) return $this->jsonJump(AdminErrCode::PARAM_ERROR,AdminErrMsg::PARAM_ERROR.','.$validate->getError());
                if($this->toolModel->addTool($data) === true) return $this->jsonJump(AdminErrCode::SUCCESS,AdminErrMsg::SUCCESS);
                return $this->jsonJump(AdminErrCode::ADD_TOOL_FAIL,AdminErrMsg::ADD_TOOL_FAIL);        
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->uid,$e,$this->request->path());
            return $this->jsonJump(AdminErrCode::SYSTEM_ERROR,AdminErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }

    /**
     * 删除工具
     * @param int $toolid
     */
    public function delete()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Tool');
                $check = $validate->scene('delete')->check($data);
                if(!$check) return $this->jsonJump(AdminErrCode::PARAM_ERROR,AdminErrMsg::PARAM_ERROR.','.$validate->getError());
                if($this->toolModel->deleteTool($data['toolid']) === true) return $this->jsonJump(AdminErrCode::SUCCESS,AdminErrMsg::SUCCESS);
                return $this->jsonJump(AdminErrCode::DELETE_TOOL_FAIL,AdminErrMsg::DELETE_TOOL_FAIL);    
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->uid,$e,$this->request->path());
            return $this->jsonJump(AdminErrCode::SYSTEM_ERROR,AdminErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }

    /**
     * 编辑
     * @param int    $toolid    工具id值
     * @param string $method    状态开关:statusButton 可见开关:visibleButton 编辑信息:editInfo 排序:sortTool
     * statusButton     status 状态开关:1->开启:正常使用 2->关闭:用户点击后，该功能提示【正在开发中，暂时还不能使用】
     * visibleButton    visible 1->开启:用户可见 2->关闭:用户不可见
     * editInfo
     * sortTool         old 原始位置  0 第一个 新位置    1 移动到第二个
     */
    public function edit()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Tool');
                $check = $validate->scene('editMethod')->check($data);
                if(!$check) return $this->jsonJump(AdminErrCode::PARAM_ERROR,AdminErrMsg::PARAM_ERROR.','.$validate->getError());
                switch ($data['method']) {
                    case 'statusButton':
                        $check = $validate->scene('statusButton')->check($data);
                        if(!$check) return $this->jsonJump(AdminErrCode::PARAM_ERROR,AdminErrMsg::PARAM_ERROR.','.$validate->getError());
                        if($this->toolModel->editToolStatus($data) === true){
                            return $this->jsonJump(AdminErrCode::SUCCESS,AdminErrMsg::SUCCESS);    
                        }
                        return $this->jsonJump(AdminErrCode::HANDLE_TOOL_FAIL,AdminErrMsg::HANDLE_TOOL_FAIL);
                        break;
                    case 'visibleButton':
                        $check = $validate->scene('visibleButton')->check($data);
                        if(!$check) return $this->jsonJump(AdminErrCode::PARAM_ERROR,AdminErrMsg::PARAM_ERROR.','.$validate->getError());
                        if($this->toolModel->editToolVisible($data) === true){
                            return $this->jsonJump(AdminErrCode::SUCCESS,AdminErrMsg::SUCCESS);    
                        }
                        return $this->jsonJump(AdminErrCode::HANDLE_TOOL_FAIL,AdminErrMsg::HANDLE_TOOL_FAIL);
                        break;
                    case 'editInfo':
                        $check = $validate->scene('editInfo')->check($data);
                        if(!$check) return $this->jsonJump(AdminErrCode::PARAM_ERROR,AdminErrMsg::PARAM_ERROR.','.$validate->getError());
                        if($this->toolModel->editToolInfo($data) === true){
                            return $this->jsonJump(AdminErrCode::SUCCESS,AdminErrMsg::SUCCESS);    
                        }
                        return $this->jsonJump(AdminErrCode::HANDLE_TOOL_FAIL,AdminErrMsg::HANDLE_TOOL_FAIL);
                        break;
                    case 'sortTool':
                        $check = $validate->scene('sortTool')->check($data);
                        if(!$check) return $this->jsonJump(AdminErrCode::PARAM_ERROR,AdminErrMsg::PARAM_ERROR.','.$validate->getError());
                        if($this->toolModel->sortTool((int)$data['old'],(int)$data['new']) === true){
                            return $this->jsonJump(AdminErrCode::SUCCESS,AdminErrMsg::SUCCESS);    
                        }
                        return $this->jsonJump(AdminErrCode::HANDLE_TOOL_FAIL,AdminErrMsg::HANDLE_TOOL_FAIL);
                    default:
                        return $this->jsonJump(AdminErrCode::PARAM_ERROR,AdminErrMsg::PARAM_ERROR);
                        break;
                }         
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->uid,$e,$this->request->path());
            return $this->jsonJump(AdminErrCode::SYSTEM_ERROR,AdminErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }

    /**
     * 列表
     * @param int       $catid
     * @param string    $name
     * @param int       $pagesize
     * @param int       $page
     */
    public function index()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                return $this->jsonJump(AdminErrCode::SUCCESS,AdminErrMsg::SUCCESS,$this->toolModel->list($data));    
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->uid,$e,$this->request->path());
            return $this->jsonJump(AdminErrCode::SYSTEM_ERROR,AdminErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }
}